package com.xiyi.shouxin.controller;

import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.xiyi.shouxin.controller.pojo.ListVideoParams;
import com.xiyi.shouxin.entity.admin.Admin;
import com.xiyi.shouxin.entity.user.Video;
import com.xiyi.shouxin.exception.ErrorCode;
import com.xiyi.shouxin.init.CacheUtil;
import com.xiyi.shouxin.service.VideoService;
import com.xiyi.shouxin.utils.ExceptionUtil;
import com.xiyi.shouxin.view.CommonModel;
import com.xiyi.shouxin.view.pojo.ItemPage;
import com.xiyi.shouxin.view.pojo.VideoView;

@CrossOrigin("*")
@RequestMapping("/video")
@RestController
public class VideoController {
    private Gson gson = new Gson();
    @Autowired
    private Logger logger;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private VideoService videoService;

    @PostMapping("/list_video")
    public CommonModel listVideo(@RequestBody ListVideoParams params) {
        if (params == null || params.getPage() == null || params.getSize() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            ItemPage<VideoView> info = videoService.findVideoList(params.getName(),
                    params.getPage(),
                    params.getSize());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("{}查询视频列表{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/create_video")
    public CommonModel createVideo(@RequestBody Video params) {
        if (params == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            VideoView info = videoService.createVideo(params);
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("{}新增视频{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/update_video")
    public CommonModel updateVideo(@RequestBody Video params) {
        if (params == null || params.getId() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            videoService.updateVideo(params);
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("{}修改视频{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/del_video")
    public CommonModel delVideo(@RequestBody List<Long> ids) {
        try {
            videoService.delVideos(ids);
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("{}删除视频{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(ids), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/info_video")
    public CommonModel infoVideo(@RequestBody Admin params) {
        if (params == null || params.getId() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            VideoView info = videoService.findVideo(params.getId());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("{}查询视频{}详情异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }
}
